package com.example.front.controller;

import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.front.dto.BoxDto;
import com.example.front.dto.IncidentDto;
import com.example.front.dto.dataRport.DataReportDto;
import com.example.front.entity.Box;
import com.example.front.entity.Event;
import com.example.front.entity.Incident;
import com.example.front.mapper.*;
import com.example.front.until.TimesUntil;
import com.example.interceptor.UserContext;
import com.example.passport.dto.sql.Sql;
import com.example.passport.entity.Tenant;
import com.example.passport.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: scx
 * @Date: 2022/5/4 14:30
 */

@Api(tags = "数据报表")
@RestController
@CrossOrigin
@RequestMapping("/front/dataReport")
public class DataReportController {

    @Resource
    IncidentDayMapper incidentDayMapper;
    @Resource
    IncidentMonthMapper incidentMonthMapper;
    @Resource
    IncidentQuarterMapper incidentQuarterMapper;
    @Resource
    IncidentYearMapper incidentYearMapper;
    @Resource
    IncidentMapper incidentMapper;
    @Resource
    EventMapper eventMapper;
    @Resource
    BoxMapper boxMapper;

    @ApiOperation("获取当天数据")
    @PostMapping("/days")
    public JsonResult<List<IncidentDto>> daysReport(DataReportDto dataReportDto) {

        User user = UserContext.getUser();
        Long tenantId = user.getTenantId();
        dataReportDto.setTenantId(tenantId);

        List<DataReportDto> dataReportDtos = new ArrayList<>();

        if (dataReportDto.getFlag() == 1){ //日
            dataReportDto.setDaysStart(TimesUntil.nowDay());
            dataReportDto.setDaysEnd(TimesUntil.nowDay());
            dataReportDtos = incidentDayMapper.find(dataReportDto);
        }else if(dataReportDto.getFlag() == 2){ //月
            dataReportDto.setDaysStart(TimesUntil.nowYearMonth() + "-01");
            String lastDayOfMonth = TimesUntil.getLastDayOfMonth(Integer.parseInt(TimesUntil.nowMonth()));
            String last = lastDayOfMonth.split(" ")[0];
            dataReportDto.setDaysEnd(last);
            dataReportDtos = incidentMonthMapper.find(dataReportDto);
        }else if(dataReportDto.getFlag() == 3){ //季
            int month = Integer.parseInt(TimesUntil.nowMonth());
            String first = TimesUntil.getFirstDayOfQuarter(month).split(" ")[0];
            dataReportDto.setDaysStart(first);
            String last = TimesUntil.getLastDayOfQuarter(month).split(" ")[0];
            dataReportDto.setDaysEnd(last);
            dataReportDtos = incidentQuarterMapper.find(dataReportDto);
        }else{ //年
            String year = TimesUntil.nowYear();
            dataReportDto.setDaysStart(year + "-01-01");
            dataReportDto.setDaysEnd(year + "-12-31");
            dataReportDtos = incidentYearMapper.find(dataReportDto);
        }

        List<Long> boxIds = new ArrayList<>();
        List<IncidentDto> incidentDtoList = new ArrayList<>();

        if (dataReportDtos != null && dataReportDtos.size() > 0){

            for (DataReportDto reportDto : dataReportDtos) {
                boxIds.add(reportDto.getBoxId());
            }

            Incident incident = new Incident();
            incident.setBoxIds(boxIds);
            incident.setTenantId(tenantId);
            incident.setDateStart(dataReportDto.getDaysStart() + " 00:00:00");
            incident.setDateEnd(dataReportDto.getDaysEnd() + " 23:59:59");
            incidentDtoList = incidentMapper.findByBoxId(incident);

            if (incidentDtoList.size() > 0){
                for (int i1 = 0; i1 < incidentDtoList.size(); i1++) {
                    Event event = eventMapper.findById(tenantId, incidentDtoList.get(i1).getEventId());
                    if (event != null){
                        incidentDtoList.get(i1).setEventName(event.getName());
                    }
                    Box box = boxMapper.findById(tenantId, incidentDtoList.get(i1).getBoxId());
                    if (box != null){
                        incidentDtoList.get(i1).setBoxName(box.getName());
                    }
                }
            }
        }

        return new JsonResult<>(incidentDtoList);
    }
    
    
    
}
